package com.rimi.service.impl;

import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.rimi.bean.User;
import com.rimi.bean.Video;
import com.rimi.cache.VideoCache;
import com.rimi.cache.AdminCache;
import com.rimi.dao.VideoDao;
import com.rimi.service.VideoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.naming.Name;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * InnoDB free: 11264 kB; (`user_id`) REFER `owl_video/user`(`user_id`); (`menu_id` 服务实现类
 * </p>
 *
 * @author sky
 * @since 2018-06-06
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoDao, Video> implements VideoService {
    @Autowired
    private VideoDao videoDao;
    @Autowired
    private AdminCache adminCache;
    @Autowired
    private VideoService videoService;
    @Autowired
    private VideoCache videoCache;


    @Override
    public Video getVideoById(Integer id) {
        Video v = videoCache.getVideoByIdFromCache(id);
        if(v!=null){
            return v;
        }else {
            v=videoDao.getVideoById(id);
            videoCache.addVideo(v);
        }
        return v;
    }


    //查询视频菜单获取图片列表
    @Override
    public List<Video> getVideoList() {
        List<Video> videoList = adminCache.getCacheList("Videos",Video.class);
        //缓存中是否有数据
        if(videoList!=null&&!videoList.isEmpty()){
            return videoList;
        }
        //如果没有数据
        Page<Video> page= videoService.selectPage(new Page<Video>(1, 6), null);
        List<Video> videoList1 = page.getRecords();
        //将查询结果缓存到redis中
        adminCache.addCacheList("Videos", videoList1);
        return videoList1;

    }

    @Override
    public void collectVideo(Integer userId, Integer videoId) {
        videoDao.collectVideo(userId,videoId);
    }

    @Override
    public void hotUp(Video v) {
        v.setVideoHot(v.getVideoHot()+1);
        videoCache.addVideo(v);
    }


    @Override
    public int selectCount(String name) {
        if(name==null){
            name="";
            String pageNum1 = (String) adminCache.getCacheObj("pageNum");
            if(pageNum1!=null&&!"".equals(pageNum1)){
                int num = Integer.parseInt(pageNum1);
                return num;
            }
        }

        int num1 = videoDao.selectCount("%"+name+"%");
        adminCache.addCacheValue("pageNumSearchPage", num1);
        return num1;
    }

    @Override
    public List selectLike(String name,Integer num1) {
        if(name==null){
            name="";
            List cacheList = adminCache.getCacheList(name, Video.class);
            if (cacheList!=null&&!cacheList.isEmpty()){
                return cacheList;
            }
        }
        List<Video> list = videoDao.selectLike("%"+name+"%", num1);
        adminCache.addCacheList(name, list);
        return list;
    }

    @Override
    public List<Video> getVideoByVideoMenu(String video_menu) {
        List<Video> VideoList = adminCache.getCacheList(video_menu, Video.class);
        if (VideoList!=null&&!VideoList.isEmpty()){
            return VideoList;
        }
        List<Video> videoList = videoService.selectList(new EntityWrapper<Video>().eq("video_menu", video_menu));
        adminCache.addCacheList(video_menu, videoList);
        return videoList;
    }

    @Override
    public void addVideoPlayInfo(User user, Integer videoId, String nowTime) {
        videoCache.addVideoPlayInfo(user,videoId,nowTime);
    }

    @Override
    public HashMap<Integer, String> getPlayInfoByIdFromCache(Integer userId) {
        return videoCache.getVideoPlayInfo(userId);
    }

    @Override
    public int selectCount1(String name) {
        String pageNumListPage = (String) adminCache.getCacheObj("pageNumListPage");
        if (pageNumListPage!=null&&!"".equals(pageNumListPage)){
            int num = Integer.parseInt(pageNumListPage);
            return num;
        }
        int i = videoDao.selectCount1(name);
        adminCache.addCacheValue("pageNumListPage", i+"");
        return i;
    }

    @Override
    public List selectLike1(String name, Integer num1) {
        List cacheList = adminCache.getCacheList(name, Video.class);
        if (cacheList!=null&&!cacheList.isEmpty()){
            return cacheList;
        }
        List list = videoDao.selectLike1(name, num1);
        adminCache.addCacheList(name, list);
        return list;
    }


}
